#___________________________________________________________________________________________________________________________________________
#___________________________________________________________________________________________________________________________________________
#___________________________________________________________________________________________________________________________________________
inherit CometCompo_evolution_PM_P_U_basic_user_eval PM_U_Container

#___________________________________________________________________________________________________________________________________________
method CometCompo_evolution_PM_P_U_basic_user_eval constructor {name descr args} {
 this inherited $name $descr
   this set_GDD_id FUI_CometCompo_evolution_PM_P_U_basic_user_eval

   set this(nb_visualisations) 0
   set this(L_C_cont) [list]
   
   # COMET hierarchy to provide ways for user to evaluate individual candidates
   set this(C_root_eval)       [CPool get_a_comet CometContainer -set_name "Root container for the evaluation component"]
   set this(C_daughter_handle) [CPool get_a_comet CometContainer -set_name "Handle for out daughters"]
   set this(C_act_validate) [CPool get_a_comet CometActivator -set_text "Validate scores and continue" -set_name "Validate score and continue" \
                                                              -Subscribe_to_activate $objName "$objName Validate_scores" \
															  -Add_style_class [list ACT VALIDATE]]
   

   set this(C_inter)     [CPool get_a_comet CometInterleaving]

   $this(C_root_eval) set_daughters_R [list $this(C_act_validate) $this(C_inter) $this(C_daughter_handle)]
   
   this set_L_nested_handle_LM    $this(C_root_eval)_LM_LP
   this set_L_nested_daughters_LM $this(C_daughter_handle)_LM_LP
   
 eval "$objName configure $args"
 return $objName
}

#___________________________________________________________________________________________________________________________________________
Methodes_set_LC CometCompo_evolution_PM_P_U_basic_user_eval [P_L_methodes_set_CometCompo_evolution] {} {}
Methodes_get_LC CometCompo_evolution_PM_P_U_basic_user_eval [P_L_methodes_get_CometCompo_evolution] {$this(FC)}

#___________________________________________________________________________________________________________________________________________
Generate_PM_setters CometCompo_evolution_PM_P_U_basic_user_eval [P_L_methodes_set_CometCompo_evolution_COMET_RE]

#___________________________________________________________________________________________________________________________________________
Generate_accessors CometCompo_evolution_PM_P_U_basic_user_eval [list C_act_validate C_inter]

#___________________________________________________________________________________________________________________________________________
#___________________________________________________________________________________________________________________________________________
#___________________________________________________________________________________________________________________________________________
method CometCompo_evolution_PM_P_U_basic_user_eval set_LM {LM} {
 set rep [this inherited $LM]
   this set_nb_visualisations 16
 return $rep
}

#___________________________________________________________________________________________________________________________________________
#___________________________________________________________________________________________________________________________________________
#___________________________________________________________________________________________________________________________________________
method CometCompo_evolution_PM_P_U_basic_user_eval set_nb_visualisations {nb} {
# Release existing visualisation that are in plus
 foreach C [lrange $this(L_C_cont) $nb $this(nb_visualisations)] {
    
   lassign $C C_top C_version C_Palette
   $this(C_inter) Sub_daughters_R $C_top
   CPool Release_a_comet $C_top
   CPool Release_a_comet $C_Palette 
   CPool Release_a_comet $C_version
  }
 set this(L_C_cont) [lrange $this(L_C_cont) 0 [expr $nb - 1]]
  
# Create missing visualisations
 for {set i $this(nb_visualisations)} {$i < $nb} {incr i} {
   set version [expr $i + 1]
   set C_top     [CPool get_a_comet CometContainer -Add_style_class "CONTAINER TOP [expr $i + 1]" -set_name "Evaluation of\nversion $version"]
#   set C_text    [CPool get_a_comet CometText      -Add_style_class "TEXT CSS++"]
#   set C_mark   [CPool get_a_comet CometChoiceN   -Add_style_class "CHOICE NCHOICE CHOICEN SCORE VERSION [expr $i + 1]" -set_name "Score of version $version" -set_b_inf 1 -set_b_sup 5 -set_val 3]
   # ${C_score}_LM_LP set_PM_factories [Generate_factories_for_PM_type [list {ChoiceN_PM_P_menu_HTML Ptf_HTML} \
                                                                     # ] ${C_score}_LM_LP]
   set C_version [CPool get_a_comet CometMarker -Add_style_class "MARKER VERSION [expr $i + 1]" -set_name "Marker of\nversion $version"]

       set C_Palette [CPool get_a_comet CometContainer -Add_style_class "PALETTE $version" \
												-set_name "Palette of Individu : $version" \
												-Add_daughters_R [list 	[CPool get_a_comet CometText  -Add_style_class  "COLOR C1"  -set_name "Palette $version C1" -set_text "C1"] \
																		[CPool get_a_comet CometText  -Add_style_class  "COLOR C2"  -set_name "Palette $version C2" -set_text "C2"] \
																		[CPool get_a_comet CometText  -Add_style_class  "COLOR C3"  -set_name "Palette $version C3" -set_text "C3"] \
																		[CPool get_a_comet CometText  -Add_style_class  "COLOR C4"  -set_name "Palette $version C4" -set_text "C4"] \
																		[CPool get_a_comet CometText  -Add_style_class  "COLOR C5"  -set_name "Palette $version C5" -set_text "C5"] ] \
	                 ]
   
   $C_top set_daughters_R [list $C_Palette $C_version]
   $this(C_inter) Add_daughters_R $C_top
   if {[this get_comet_root_to_be_transformed] != ""} {$C_version set_daughters_R [this get_comet_root_to_be_transformed]}
   
   lappend this(L_C_cont) [list $C_top $C_version $C_Palette]
  }
  
 set this(nb_visualisations) $nb
}

#___________________________________________________________________________________________________________________________________________
Inject_code CometCompo_evolution_PM_P_U_basic_user_eval Process {} {
# Disable the AJAX update in the cas we are plugged to a HTML root
 if {[${objName}_cou_ptf get_soft_type] == "PHP_HTML"} {set tmp_enable_AJAX_UPDATE [this get_class_enable_AJAX_UPDATE]
                                                        this set_class_enable_AJAX_UPDATE 0
													   }

# Let's apply transformations !
 set L_in [this get_Input_data in]
 this set_nb_visualisations [llength $L_in]
 #puts "  We have [llength $L_in] stylesheets"

# Clean all !
 #puts "Cleaning previous presentations !  \"#${objName}(CONTAINER.VERSION *)\""
 set L_PMs [CSS++ $objName "#${objName}(MARKER.VERSION *)"]
 #puts "  => $L_PMs"
 foreach PM $L_PMs {
   if {[catch {$PM dispose} err]} {puts "  Error while killing $PM : $err"}
  }
 #puts "... cleaning done !"

 #puts "<Replug>"
 foreach version $this(L_C_cont) {
   lassign $version C_top C_version C_Palette
   set C [$C_version get_out_daughters]
   $C_version set_daughters_R $C
  }
 #puts "</Replug>"	

 set i 0
 foreach CV [$this(C_inter) get_out_daughters] {
   

   
   set individu [lindex $L_in $i]
   set score    [$individu get_mark];
   $CV Add_MetaData INDIVIDU $individu
   [CSS++ $CV "#$CV MARKER.VERSION"] set_mark [expr $score > 0.5]
   
   
   set CV_PM_TOP  [CSS++ $objName "#${objName}($CV TOP)"]
   if {[llength $CV_PM_TOP] > 1} {
	 puts stderr "Warning CSS++ $objName #${objName}($CV TOP) ; return more than 1 element $CV_PM_TOP"
	 set CV_PM_TOP [lindex $CV_PM_TOP 1]
   }
	if {[catch { $CV_PM_TOP set_name "Evaluation of individu $individu"
   $CV_PM_TOP set_title "Evaluation of individu $i ($individu)" 
  
   } {err} ] } {puts stderr "$CV_PM_TOP : $err \n CSS++ $objName #${objName}($CV TOP)" ; puts [CSS++ $objName "#${objName}($CV TOP)"]  } 
   set L_color [$individu get_L_color]
   foreach color $L_color text [CSS++ $CV_PM_TOP "COLOR"] {
	lassign [MAGELLAN__getBetterContrast $color $L_color] mod ind 
		if {[catch {$text bg_fg [list uniform $color] [MAGELLAN__COLOR_MUTATOR $mod [lindex $L_color $ind]]} err]} {
		  error "ERROR in CometCompo_evolution_PM_P_U_basic_user_eval::Process while getting colors\n  individu : $individu"
		 }
   }
   
   set C_version [CSS++ $CV "#${objName}($CV MARKER.VERSION)"] 

   # <TO_BE_TESTED>
     # Apply style on the version i, for now it only display selector and their result.

	 set PM_root [CSS++ $objName "#$C_version [this get_comet_root_to_be_transformed]"]

	# Apply the style
	 Update_style_parsed_CSS [this get_DSL_GDD_QUERY] \
	                         [this get_DSL_CSSpp]     \
							 [list [list Implem {NODE()<-REL(type~=GDD_inheritance && type!=GDD_restriction)*<REL(type~=GDD_implementation)<-$n()}]] \
							 [$individu Serialize_to_parsed_CSS $PM_root] \
							 ""
   # </TO_BE_TESTED>
   
   incr i
  }

# Disable the AJAX update in the cas we are plugged to a HTML root
 if {[${objName}_cou_ptf get_soft_type] == "PHP_HTML"} {this set_class_enable_AJAX_UPDATE $tmp_enable_AJAX_UPDATE
                                                        set cmd ""
														foreach d [this get_daughters] {
														  append cmd [this Add_JS $d]
														 }
														$this(PM_root) send_jquery_message ALL_UPDATE $cmd
													   }
	foreach daughter [[[$this(C_inter) get_L_LM] get_L_actives_PM] get_daughters] {
		$daughter add_html_style [list "display" "inline-block"] 
		$daughter add_html_style [list "width" "49%"] 
		$daughter add_html_style [list "height" "400px"]  
	}
}

Trace CometCompo_evolution_PM_P_U_basic_user_eval Process

#___________________________________________________________________________________________________________________________________________
method CometCompo_evolution_PM_P_U_basic_user_eval Validate_scores {} {
 set L_in [this get_Input_data in]; set i 0
 foreach CV [$this(C_inter) get_out_daughters] {
   set individu [lindex $L_in $i]
   set mark  [[CSS++ $CV "#$CV MARKER"] get_mark]
   $individu set_mark $mark
   incr i
  }
  
 this prim_set_Output_data out $L_in
 this prim_set_has_processed 1
}

